package org.example.iot;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioChannelOption;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Test0x0101 {

    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup worker = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.group(worker);
        bootstrap.option(NioChannelOption.TCP_NODELAY, true);
        bootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel ch) throws Exception {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                    //和服务器建立连接之后的回调
                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("连接建立");
                        /**
                         * 上传数据包
                         *
                         * 02AFCBD65F006D01014457475A475A4D323230353134303144322E30302E3030303338393836303442303130323134303432303532391900002C01073BCAD65FCBA13D401B2FDD3E0000000000000000000000000000000000000000000000000000000000000000E600E6000C00080060EA000006AF0D0A
                         */
                        while (true){
                            byte[] bytes1 = ByteBufUtil.decodeHexDump("02AFCBD65F006D01014457475A475A4D323230353134303144322E30302E3030303338393836303442303130323134303432303532391900002C0107");
                            int time = (int) (new Date().getTime() / 1000);
                            byte[] bytes3 = ByteBufUtil.decodeHexDump("CBA13D401B2FDD3E0000000000000000000000000000000000000000000000000000000000000000E600E6000C00080060EA000006AF0D0A");
                            ByteBuf buffer = Unpooled.buffer();
                            buffer.writeBytes(bytes1);
                            buffer.writeIntLE(time);
                            buffer.writeBytes(bytes3);
                            ctx.writeAndFlush(buffer);
                            Thread.sleep(1200);
                        }

                    }
                });
            }
        });
        ChannelFuture channelFuture = bootstrap.connect("iottcp.caups.cn", 9876).sync();
        Channel channel = channelFuture.channel();

    }

    public static byte[] crc16CCITTFalse(byte[] bytes) {
        int crc = 0xffff; // initial value
        int polynomial = 0x1021; // poly value
        for (int index = 0; index < bytes.length; index++) {
            byte b = bytes[index];
            for (int i = 0; i < 8; i++) {
                boolean bit = ((b >> (7 - i) & 1) == 1);
                boolean c15 = ((crc >> 15 & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit){
                    crc ^= polynomial;
                }
            }
        }
        crc &= 0xffff;
        return new byte[] { (byte) ((0xff00 & crc) >> 8),(byte) (0xff & crc)  };
    }

}
